from binance.client import Client
import requests
import datetime

proxies = {
    'http': '127.0.0.1:7897',
    'https': '127.0.0.1:7897'
}

api_key = 'JdBXaCjTg2SM5bX9kz68ZZXU0QBCktallwauB3QsRJRgl3bAnT9ZOdZqmSlB0thn'
api_secret = 'GB0aYB7QybL4IxwdA2UrU5yst4FXxkbtMFBADYxNs6qShNQFNSBVfBEER8C4l6SR'

client = Client(api_key, api_secret, {'proxies': proxies})

# 设置交易对和时间范围
symbol = 'ETHBTC'
interval = Client.KLINE_INTERVAL_4HOUR
interval_day = Client.KLINE_INTERVAL_1DAY
# 删除原时间范围设置，改为直接获取最近1000条数据
end_time = datetime.datetime.now()

# 获取历史 K 线数据（添加limit参数获取最近1000条）
klines = client.get_historical_klines(
    symbol=symbol,
    interval=interval,
    end_str=end_time.strftime("%Y-%m-%d %H:%M:%S"),
    limit=1000  # 新增limit参数限制数量
)

# 输出每条 K 线的开盘价、最高价、最低价、收盘价和成交量
print(type(klines))
for kline in klines:
    open_time = datetime.datetime.fromtimestamp(kline[0] / 1000)
    open_price = kline[1]
    high_price = kline[2]
    low_price = kline[3]
    close_price = kline[4]
    volume = kline[5]
    print(f"时间: {open_time}, 开盘价: {open_price}, 最高价: {high_price}, 最低价: {low_price}, 收盘价: {close_price}, 成交量: {volume}")